<template>
	<view class="makesureorder">
		<view class="makesureorder_top">
			<image src="https://hxc-pay.oss-cn-shenzhen.aliyuncs.com/RyXxAytMWf_red_icon.png" class="img_icon" mode="">
			</image>
			<view class="slip-title">
				<view class="img">
					<image src="https://hxc-pay.oss-cn-shenzhen.aliyuncs.com/SSdTpLApdQ_goto_icon_white.png" mode=""
						@click="gotoBack"></image>
				</view>
				<view class="txt">确认订单</view>
			</view>
			<view class="address_box" @click="choseAddres" v-if="newDev.length > 0">
				<image src="https://hxc-pay.oss-cn-shenzhen.aliyuncs.com/NyWVVmCbyK_address_icon.png"
					class="img_address" mode=""></image>
				<view class="contanta_box">
					<view class="address_txt">
						<text class="my-mgr-30">{{newDev[0].name}}</text>
						<text>{{newDev[0].phone}}</text>
					</view>
					<view class="contant">
						{{newDev[0].province}}{{newDev[0].city}}{{newDev[0].area}}{{newDev[0].address}}</view>
				</view>
				<image src="https://hxc-pay.oss-cn-shenzhen.aliyuncs.com/FaFnYvYsKD_rightarrow_icon.png"
					class="img_arrow" mode=""></image>
			</view>
			<view class="address_box" v-else @click="goaddAddres">
				<view class="contanta_box">请添加收货地址</view>
				<image src="https://hxc-pay.oss-cn-shenzhen.aliyuncs.com/FaFnYvYsKD_rightarrow_icon.png"
					class="img_arrow" mode=""></image>
			</view>
		</view>
		<view class="goods_box my-mgt-25">
			<view class="fir_box my-pdr-5">
				<image class="img" :src="list[0]" mode=""></image>
				<view class="fir_bo_tant">
					<view class="txt">{{name}}</view>
					<view class="money my-mgt-10">￥{{price}}</view>
				</view>
				<view class="num">x{{num}}</view>
			</view>
			<view class="inp_box">
				<view class="inp_txt">买家留言</view>
				<textarea class="area" v-model.lazy="levenTxt" placeholder-class="pla" placeholder="填写备注内容45字以上" />
			</view>
		</view>
		<view class="pay_box my-mgt-10 my-pdl-15">
			<!-- <view class="pay_box_item">
				<image class="pay_img" src="https://hxc-pay.oss-cn-shenzhen.aliyuncs.com/zxxqZuXySj_balance_icon.png" mode=""></image>
				<view class="pay_box_txt my-mgl-10">余额支付</view>
				<radio style="transform: scale(0.6);" @click="changeRadio('BALANCE')" color="#F05329" value="BALANCE" :checked="payType==='BALANCE'"/>
			</view> -->
			<view class="pay_box_item">
				<image class="pay_img" src="https://hxc-pay.oss-cn-shenzhen.aliyuncs.com/NURXSBJagV_wx_icon.png"
					mode=""></image>
				<view class="pay_box_txt my-mgl-10">微信支付</view>
				<radio style="transform: scale(0.6);" @click="changeRadio('WX')" color="#F05329" value="WX"
					:checked="payType==='WX'" />
			</view>
		</view>
		<view class="detail_box my-mgt-10 my-pdlr-15">
			<view class="detail_box_li">
				<view class="detail_box_li_txt">商品金额</view>
				<view class="detail_box_li_money">￥{{price * num - 0}}</view>
			</view>
			<view class="detail_box_li" v-if="upgradeDev.length > 1">
				<view class="detail_box_li_txt">购机券</view>
				<view class="name_box" v-if="upgradeitem" @click="upgrade=true">
					<text class="orig_txt my-mgr-10">{{upgradeDev.length}}张可用</text>
					<image class="img_gouji"
						src="https://hxc-pay.oss-cn-shenzhen.aliyuncs.com/FaFnYvYsKD_rightarrow_icon.png" mode="">
					</image>
				</view>
				<view class="name_box" v-else @click="upgrade=true">
					<text class="orig_txt my-mgr-10">-￥{{upgradenum}}</text>
					<image class="img_gouji"
						src="https://hxc-pay.oss-cn-shenzhen.aliyuncs.com/FaFnYvYsKD_rightarrow_icon.png" mode="">
					</image>
				</view>
			</view>
			<view class="detail_box_li">
				<view class="detail_box_li_txt">配送方式</view>
				<view class="detail_box_li_money">寄送</view>
			</view>
		</view>
		<view class="txt_red my-pdl-15">*当前选择支付方式起购数为{{upgradenum===null?moneyStartQuantity:couponsStartQuantity}}台
		</view>
		<view class="lint_btn my-pdlr-15">
			<view class="left_box">
				<text>合计：<text
						class="money_box">￥{{upgradenum===null?price * num - 0: (((price * num) - upgradenum - 0)>=0?(price * num) - upgradenum - 0:0)}}</text></text>
			</view>
			<view class="right_box">
				<button class="btn_liji" @click="submitGoods">提交订单</button>
			</view>
		</view>
		<u-popup v-model="upgrade" mode="bottom" border-radius="40">
			<view class="upgrade_box">
				<view class="upgrade_box_tlt">
					<view class="name my-pdl-20">购机券</view>
					<image src="https://hxc-pay.oss-cn-shenzhen.aliyuncs.com/CoNFXgIOmf_open_icon.png"
						style="width: 28rpx;height: 28rpx;" mode=""></image>
				</view>
				<scroll-view scroll-y="true" class="scroll_position">
					<template v-for="(item,index) in upgradeDev">
						<view class="upgrad_box_li my-mgt-10" :key="index">
							<image class="upgrad_img" src="https://hxc-pay.oss-cn-shenzhen.aliyuncs.com/IesCoVdktV_dont_use.png"
								mode=""></image>
							<view class="contant_item my-pdl-15">
								<view class="upgrad_box_li_num">
									<text class="zifu">￥</text>
									<text class="money">{{item.price||0}}</text>
								</view>
								<view class="upgrad_box_li_center">
									<view class="upgrad_box_li_center_name my-pdl-25">商品优惠券</view>
									<view class="upgrad_box_li_center_time my-mgt-5 my-pdl-25">领取时间：{{item.createDate}}</view>
								</view>
								<view class="upgrad_box_bg">
									<checkbox style="transform: scale(0.7);" @click="changeInfo(index)"
										color="#F05329" :checked="item.check" />
								</view>
							</view>
						</view>
					</template>
				</scroll-view>
				<view class="batch_box_btn">
					<button class="btn" @click="choseupgrade">确认</button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		getcouponsApi
	} from "@/api/myinter.js"
	import {
		submitOrdersApi
	} from "@/api/orders.js"
	import {
		createNamespacedHelpers
	} from "vuex";
	const {
		mapState,
		mapMutations
	} = createNamespacedHelpers(
		"address"
	);
	export default {
		data() {
			return {
				payType: 'WX',
				upgrade: false,
				num: 0,
				pageIndex: 0,
				pageSize: 30,
				total: 1,
				upgradeDev: [],
				indexType: [],
				upgradenum: null,
				upgradeitem: true,
				levenTxt: '',
				terminalPayType: 'MONEY',
				curr:false,
			};
		},
		onLoad() {
			this.num = uni.getStorageSync("goodNum")
			this.getcoupons()
		},
		mounted() {
			this.filterTrue()
		},
		// 上拉加载更多
		onReachBottom() {
			this.getcoupons()
		},
		computed: {
			...mapState(["newDev", "price", "list", "name", "couponsStartQuantity", "moneyStartQuantity"]),
		},
		methods: {
			...mapMutations(["filterTrue"]),
			async submitGoods() {
				if ((this.num < this.moneyStartQuantity && this.upgradenum === null) || (this.num < this.couponsStartQuantity && this.upgradenum === 0)) {
					uni.showToast({
						title: "购买数量应大于起购数",
						icon: "none"
					})
				} else {
					let money = this.upgradenum===null?this.price * this.num - 0: (((this.price * this.num) - this.upgradenum - 0)>=0?(this.price * this.num) - this.upgradenum - 0:0)
					if (this.upgradenum === null) {
						this.terminalPayType = 'MONEY'
					} else if (money === 0) {
						this.terminalPayType = 'COUPONS'
						this.payType = "BALANCE"
					} else {
						this.terminalPayType = 'MONEY_COUPONS'
					}
					try {
						let params = {
							applicationType: 'APPLET',
							address: this.newDev[0].address,
							area: this.newDev[0].area,
							city: this.newDev[0].city,
							province: this.newDev[0].province,
							phone: this.newDev[0].phone,
							receiverName: this.newDev[0].name,
							detail: this.levenTxt,
							expressCompany: '',
							expressNo: '',
							orderAmount: this.upgradenum===null?this.price * this.num - 0: (((this.price * this.num) - this.upgradenum - 0)>=0?(this.price * this.num) - this.upgradenum - 0:0),
							payType: this.payType,
							quantity: this.num,
							terminalGoodsId: uni.getStorageSync("id"),
							terminalPayType: this.terminalPayType,
							useCoupons: this.indexType,
							useMoney: this.upgradenum===null?this.price * this.num - 0: (((this.price * this.num) - this.upgradenum - 0)>=0?(this.price * this.num) - this.upgradenum - 0:0),
						}
						uni.showLoading({
							title: "正在加载中...."
						})
						const res = await submitOrdersApi(params)
						if (res.statusCode == 200) {
							uni.hideLoading()

							uni.showLoading({
								title: "支付中...."
							})
							if(JSON.stringify(res.data.data)==='{}'&&money===0){
								uni.hideLoading()
								uni.showToast({
									title: "支付成功!",
									icon: "success"
								})
								setTimeout(() => {
									uni.redirectTo({
										url: "/pages/my/orders?type=WAITING,CONFIRM,WAITING_SEND"
									})
								}, 1000)
							}else{
								wx.requestPayment({
									timeStamp: res.data.data.timeStamp,
									nonceStr: res.data.data.nonceStr,
									package: res.data.data.package,
									signType: 'MD5',
									paySign: res.data.data.sign,
									success: (r) => {
										uni.hideLoading()
										uni.showToast({
											title: "支付成功!",
											icon: "success"
										})
										setTimeout(() => {
											uni.redirectTo({
												url: "/pages/my/orders?type=WAITING,CONFIRM,WAITING_SEND"
											})
										}, 1000)
									},
									fail: (res) => {
										uni.hideLoading()
										if (res.errMsg.indexOf("cancel") > -1) {
											uni.showToast({
												title: "已取消支付!",
												icon: "none"
											})
											setTimeout(() => {
												uni.redirectTo({
													url: "/pages/my/orders?type=PAYING"
												})
											}, 1000)
										}
									}
								})
							}
							
						}
					} catch (e) {

					}
				}
			},
			choseupgrade() {
				if(this.curr){
					this.indexType=[]
					this.upgradenum = null
				}
				this.upgradeDev.forEach(item => {
					if (item.check) {
						this.indexType.push(JSON.stringify(item.id))
						this.upgradenum += item.price
						this.curr = true
						this.upgradeitem = false
						this.upgrade = false
					}
				})
				
			},
			// 勾选取反
			changeInfo(index) {
				this.upgradeDev[index].check = !this.upgradeDev[index].check
			},
			async getcoupons() {
				if (this.upgradeDev.length >= this.total) return
				let params = {
					pageIndex: this.pageIndex,
					pageSize: this.pageSize
				}
				uni.showLoading({
					title: "正在加载中...."
				})
				const res = await getcouponsApi(params)
				if (res.statusCode === 200) {
					uni.hideLoading()
					let data = res.data.data.rows
					this.upgradeDev = data.filter(item => item.used === false)
					this.total = res.data.data.total
					this.pageIndex++
				}
			},
			goaddAddres() {
				uni.redirectTo({
					url: "/pages/my/addAddress?type=1"
				})
			},
			// radio切换
			changeRadio(item) {
				if (item == "WX" || item == "BALANCE") {
					this.payType = item
				}
			},
			gotoBack() {
				uni.redirectTo({
					url: "/pages/mallStore/lineItem"
				})
			},
			choseAddres() {
				uni.navigateTo({
					url: "/pages/my/shippingAddress?type=1"
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.makesureorder {
		width: 100%;
		height: 100vh;
		background: #FAFAFA;
		position: relative;

		.makesureorder_top {
			width: 100%;
			height: 301rpx;
			position: relative;

			.img_icon {
				width: 100%;
				height: 301rpx;
			}

			.slip-title {
				width: 100%;
				height: 150rpx;
				display: flex;
				padding-top: 70rpx;
				position: absolute;
				top: 0rpx;
				left: 0rpx;

				.img {
					width: 15%;
					height: 100%;
					padding-top: 13rpx;

					image {
						width: 38rpx;
						height: 38rpx;
						padding-left: 29rpx;
					}
				}

				.txt {
					width: 70%;
					height: 100%;
					text-align: center;
					font-size: 37rpx;
					font-weight: bold;
					color: #FFFFFF;
				}
			}

			.address_box {
				width: 690rpx;
				height: 137rpx;
				background: #FFFFFF;
				border-radius: 10rpx;
				display: flex;
				align-items: center;
				justify-content: space-evenly;
				position: absolute;
				left: 30rpx;
				bottom: -20rpx;

				.img_address {
					width: 24rpx;
					height: 30rpx;
				}

				.img_arrow {
					width: 20rpx;
					height: 20rpx;
				}

				.contanta_box {
					width: 80%;
					height: 100%;
					display: flex;
					flex-direction: column;
					justify-content: center;

					.address_txt {
						width: 100%;
						height: 50rpx;
						font-size: 30rpx;
						font-weight: 500;
						color: #333333;
					}

					.contant {
						font-size: 24rpx;
						font-weight: 500;
						color: #999999;
					}
				}
			}

		}

		.goods_box {
			width: 690rpx;
			height: 318rpx;
			margin: auto;
			background: #FFFFFF;
			border-radius: 10rpx;

			.fir_box {
				width: 100%;
				height: 70%;
				display: flex;
				align-items: center;
				justify-content: space-evenly;

				.img {
					width: 160rpx;
					height: 160rpx;
				}

				.fir_bo_tant {
					width: 60%;

					.txt {
						font-size: 30rpx;
						font-weight: 500;
						color: #010101;
					}

					.money {
						font-size: 36rpx;
						font-weight: bold;
						color: #F05329;
					}
				}

				.num {
					width: 40rpx;
					height: 26rpx;
					font-size: 36rpx;
					font-weight: 500;
					color: #010101;
				}
			}

			.inp_box {
				width: 100%;
				height: 95rpx;
				display: flex;

				.inp_txt {
					width: 27%;
					text-align: center;
					font-size: 30rpx;
					font-weight: 500;
					color: #010101;
				}

				.pla {
					font-size: 30rpx;
					font-weight: 500;
					color: #999999;
				}

				.area {
					width: 73%;
					height: 100%;
				}
			}
		}

		.pay_box {
			width: 690rpx;
			min-height: 120rpx;
			margin: auto;
			background: #FFFFFF;
			border-radius: 10rpx;

			.pay_box_item {
				width: 100%;
				height: 100rpx;
				display: flex;
				align-items: center;

				.pay_img {
					width: 42rpx;
					height: 42rpx;
				}

				.pay_box_txt {
					width: 79%;
					font-size: 30rpx;
					font-weight: 400;
					color: #000000;
				}
			}
		}

		.detail_box {
			width: 690rpx;
			height: 250rpx;
			margin: auto;
			background: #FFFFFF;
			border-radius: 10rpx;

			.detail_box_li {
				width: 100%;
				height: 80rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.img_gouji {
					width: 17rpx;
					height: 18rpx;
				}

				.detail_box_li_txt {
					font-size: 30rpx;
					font-weight: 500;
					color: #010101;
				}

				.name_box {
					display: flex;
					align-items: center;
				}

				.orig_txt {
					font-size: 26rpx;
					font-weight: 500;
					color: #F05329;
				}

				.detail_box_li_money {
					font-size: 30rpx;
					font-weight: 500;
					color: #666666;
				}
			}
		}

		.txt_red {
			width: 100%;
			height: 150rpx;
			line-height: 150rpx;
			font-size: 26rpx;
			font-weight: 500;
			color: #F05329;
		}

		.lint_btn {
			width: 100%;
			height: 98rpx;
			background: #FFFFFF;
			box-shadow: 4rpx -7rpx 10rpx 0rpx rgba(0, 0, 0, 0.08);
			display: flex;
			align-items: center;
			justify-content: space-between;
			position: absolute;
			bottom: 5rpx;
			left: 0rpx;

			.left_box {
				width: 40%;
				height: 100%;
				display: flex;
				align-items: center;
				font-size: 26rpx;
				font-weight: 500;
				color: #010101;

				.money_box {
					font-size: 40rpx;
					font-weight: 500;
					color: #F05329;
				}
			}

			.right_box {
				width: 210rpx;
				height: 80rpx;

				.btn_liji {
					width: 210rpx;
					line-height: 80rpx;
					text-align: center;
					background: #F05329;
					border-radius: 40rpx;
					font-size: 32rpx;
					font-weight: 500;
					color: #FFFFFF;
				}
			}
		}

		.upgrade_box {
			width: 100%;
			height: 900rpx;
			background: #FFFFFF;

			.upgrade_box_tlt {
				width: 100%;
				height: 150rpx;
				display: flex;
				align-items: center;

				.name {
					width: 90%;
					text-align: center;
					font-size: 34rpx;
					font-weight: 500;
					color: #000000;
				}
			}
			.scroll_position {
				height: 640rpx;
				overflow-y: auto;
			}
			.batch_box_btn{
				width: 100%;
				height: 100rpx;
				display: flex;
				align-items: center;
				.btn{
					width: 80%;
					height: 70rpx;
					margin: auto;
					background: #F05329;
					border-radius: 10rpx;
					font-size: 28rpx;
					font-weight: 500;
					color: #FEFEFE;
				}
			}
			.upgrad_box_li {
				width: 660rpx;
				height: 168rpx;
				margin: auto;
				position: relative;
				overflow: hidden;

				.upgrad_img {
					width: 100%;
					height: 100%;
				}

				.contant_item {
					width: 100%;
					height: 100%;
					display: flex;
					align-items: center;
					position: absolute;
					left: 0rpx;
					top: 0rpx;

					.upgrad_box_li_num {
						width: 27%;
						font-weight: bold;
						color: #F05329;

						.zifu {
							font-size: 32rpx;
						}

						.money {
							font-size: 70rpx;
						}
					}

					.upgrad_box_li_center {
						width: 90%;

						.upgrad_box_li_center_name {
							font-size: 32rpx;
							font-weight: bold;
							color: #4E4142;
						}

						.upgrad_box_li_center_time {
							font-size: 22rpx;
							font-weight: 500;
							color: #654C4C;
						}
					}

					.upgrad_box_bg {
						width: 24%;

						.upgrad_box_bg_btn {
							width: 120rpx;
							height: 55rpx;
							line-height: 55rpx;
							text-align: center;
							border-radius: 28px;
							border-radius: 28rpx;
							font-size: 26rpx;
							font-weight: 500;
							color: #FFFFFF;
						}
					}
				}

			}
		}
	}
</style>
